<?php


class WebPush {
    //sockect进程
    public $webSockectServer;
    //表格用于存储数据
    public $swooleTable;
    //在线人数
    public $onlineNum = 0;

    public function __construct() {
        $this->webSockectServer = new swoole_websocket_server('0.0.0.0', 2401);
        //新建表格
        $this->swooleTable = new \swoole_table(1024);
        $this->swooleTable->column('user_id', \swoole_table::TYPE_INT, 10);
        $this->swooleTable->column('fd', \swoole_table::TYPE_INT, 4);
        $this->swooleTable->column('user_name', \swoole_table::TYPE_STRING, 30);
        $this->swooleTable->column('tage', \swoole_table::TYPE_STRING, 10);
        $this->swooleTable->column('add_time', \swoole_table::TYPE_INT, 11);
        $this->swooleTable->create();
        
        $this->webSockectServer->on('open', function($server, $request) {
            echo "客户端连接成功,fd:$request->fd";
        });
        
        $this->webSockectServer->on('message', function ($server, $frame){
            $get_data = json_decode($frame->data);
            switch ($get_data->type) {
                case 'login':
                    $data = array(); 
                    $data['user_id'] = $get_data->user_id;
                    $data['fd'] = $frame->fd;
                    $data['user_name'] = $get_data->user_name;
                    $data['tage'] = $get_data->tage;
                    $data['add_time'] = time();
                    $this->swooleTable->set($get_data->user_id, $data);
                    $this->onlineNum = $this->swooleTable->count();
                    $server->push($frame->fd, $this->responData(['fd_id' => $frame->fd, 'onlineNum' => $this->onlineNum], '收到客户端发来的消息'));
                    break;
                case 'say':
                    $data = $this->swooleTable->get($frame->fd);
                    print_r($data);
                    $server->push($frame->fd, $this->responData($data));
                    break;
                default :
                    $server->push($frame->fd, $this->responData($get_data,'hahaha'));
            }
        });
        
        $this->webSockectServer->on('close', function ($server, $fd) {
            foreach ($this->swooleTable as $row) {
                if($row['fd'] == $fd) {
                    print_r($row);
                    $this->swooleTable->del($row['user_id']);
                }
            }   
        });
        
        $this->webSockectServer->on('request', function ($request, $response) {
           $data = json_decode($request->rawContent(), TRUE);   //{"user_id":[1,2,3], "msg":"sssss"} 
            foreach ($data['user_id'] as $user_id) {
                $this->webSockectServer->push($this->swooleTable->get($user_id, 'fd'), $data['msg']);
            }
           $response->header('Content-Type', 'application/json');
           $response->end($this->responData($request->rawContent(), '发送成功'));
        });
        
        $this->webSockectServer->on('workerStart', function (){
            swoole_timer_tick(5000, function ($timer_id) {
                $count = $this->swooleTable->count();
                if($this->onlineNum != $count) {
                    $this->onlineNum = $count;
                    foreach ($this->webSockectServer->connections as $fd) {
                        $this->webSockectServer->push($fd, $this->responData(['onlineNum' => $this->onlineNum], '当前在线人数'));
                    }
                    print_r(1);
                }
            });
        });
        
        $this->webSockectServer->start();
    }
        
    public function responData($data, $msg='')
    {
        $respon['success'] = TRUE;
        $respon['msg'] = $msg;
        $respon['data'] = $data;
        return json_encode($respon, JSON_UNESCAPED_SLASHES);
    }
}

new WebPush();
